<template>
  <div>
    <div style="margin-bottom: 20px;">
      <el-input v-model="searchQuery" placeholder="请输入参赛名称进行查询" style="width: 200px; margin-right: 10px;" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>
    <el-button type="primary" @click="handleAdd"> 添加参赛信息</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="competitionName" label="参赛名称" width="180" />
      <el-table-column prop="level" label="级别" width="180" />
      <el-table-column prop="organizer" label="主办方" width="180" />
      <el-table-column prop="location" label="地点" width="180" />
      <el-table-column prop="competitionDate" label="比赛日期" width="180" />
      <el-table-column prop="awardLevel" label="获奖级别" width="180" />
      <el-table-column prop="participants" label="参赛者" width="180" />
      <el-table-column prop="instructor" label="指导老师" width="180" />
      <el-table-column prop="certificate" label="证书" width="180" />
      <el-table-column prop="reportDate" label="填报日期" width="180" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button round type="warning" @click="handleUpdate(scope.row)">更新</el-button>
          <el-popconfirm title="你确定要删除吗?" @confirm="handleDelete(scope.row.id)" confirm-button-text="确定" cancel-button-text="取消">
            <template #reference>
              <el-button round type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加对话框 -->
    <el-dialog v-model="dialogVisible" title="添加参赛信息">
      <el-form ref="addFormRef" :model="addForm" :rules="rules" label-width="100px" class="ruleForm" status-icon>
        <el-form-item label="参赛名称" prop="competitionName">
          <el-input v-model="addForm.competitionName"/>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="addForm.level"/>
        </el-form-item>
        <el-form-item label="主办方" prop="organizer">
          <el-input v-model="addForm.organizer"/>
        </el-form-item>
        <el-form-item label="地点" prop="location">
          <el-input v-model="addForm.location"/>
        </el-form-item>
        <el-form-item label="比赛日期" prop="competitionDate">
          <el-date-picker v-model="addForm.competitionDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="获奖级别" prop="awardLevel">
          <el-input v-model="addForm.awardLevel"/>
        </el-form-item>
        <el-form-item label="参赛者" prop="participants">
          <el-input v-model="addForm.participants"/>
        </el-form-item>
        <el-form-item label="指导老师" prop="instructor">
          <el-input v-model="addForm.instructor"/>
        </el-form-item>
        <el-form-item label="证书" prop="certificate">
          <el-input v-model="addForm.certificate"/>
        </el-form-item>
        <el-form-item label="填报日期" prop="reportDate">
          <el-date-picker v-model="addForm.reportDate" type="date" placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible=false">取消</el-button>
          <el-button type="primary" @click="handleAddConfirm">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 更新对话框 -->
    <el-dialog v-model="dialogUpdateVisible" title="更新参赛信息">
      <el-form ref="updateFormRef" :model="updateForm" :rules="rules" label-width="100px" class="ruleForm" status-icon>
        <el-form-item label="参赛名称" prop="competitionName">
          <el-input v-model="updateForm.competitionName"/>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="updateForm.level"/>
        </el-form-item>
        <el-form-item label="主办方" prop="organizer">
          <el-input v-model="updateForm.organizer"/>
        </el-form-item>
        <el-form-item label="地点" prop="location">
          <el-input v-model="updateForm.location"/>
        </el-form-item>
        <el-form-item label="比赛日期" prop="competitionDate">
          <el-date-picker v-model="updateForm.competitionDate" type="date" placeholder="选择日期"/>
        </el-form-item>
        <el-form-item label="获奖级别" prop="awardLevel">
          <el-input v-model="updateForm.awardLevel"/>
        </el-form-item>
        <el-form-item label="参赛者" prop="participants">
          <el-input v-model="updateForm.participants"/>
        </el-form-item>
        <el-form-item label="指导老师" prop="instructor">
          <el-input v-model="updateForm.instructor"/>
        </el-form-item>
        <el-form-item label="证书" prop="certificate">
          <el-input v-model="updateForm.certificate"/>
        </el-form-item>
        <el-form-item label="填报日期" prop="reportDate">
          <el-date-picker v-model="updateForm.reportDate" type="date" placeholder="选择日期"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogUpdateVisible=false">取消</el-button>
          <el-button type="primary" @click="handleUpdateConfirm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>



</template>

<script setup>
import { onMounted, reactive, ref } from "vue";
import axios from "axios";

const tableData = ref([]);
const searchQuery = ref("");

const getList = async (query = "") => {
  try {
    let res;
    if (query) {
      res = await axios.get(`http://localhost:8000/adminapi/competitions?competitionName=${query}`);
    } else {
      res = await axios.get('http://localhost:8000/adminapi/competitions');
    }
    tableData.value = res.data;
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}




const handleAdd = () => {
  dialogVisible.value = true;
}
const dialogVisible = ref(false);
const addFormRef = ref();
const addForm = reactive({
  competitionName: "",
  level: "",
  organizer: "",
  location: "",
  competitionDate: "",
  awardLevel: "",
  participants: "",
  instructor: "",
  certificate: "",
  reportDate: ""
});
const rules = reactive({
  competitionName: [
    { required: true, message: '请输入参赛名称', trigger: 'blur' },
  ]
});
const handleAddConfirm = () => {
  addFormRef.value.validate(async (valid) => {
    if (valid) {
      dialogVisible.value = false;
      await axios.post('http://localhost:8000/adminapi/competitions', addForm);
      await getList();
      Object.keys(addForm).forEach(key => addForm[key] = "");
    }
  });
}

onMounted(() => {
  getList();
});

const dialogUpdateVisible = ref(false);
const updateFormRef = ref();
const currentItem = ref();
const updateForm = reactive({
  competitionName: "",
  level: "",
  organizer: "",
  location: "",
  competitionDate: "",
  awardLevel: "",
  participants: "",
  instructor: "",
  certificate: "",
  reportDate: ""
});

const handleUpdate = (item) => {
  dialogUpdateVisible.value = true;
  Object.assign(updateForm, item);
  currentItem.value = item;
}
const handleUpdateConfirm = async () => {
  try {
    dialogUpdateVisible.value = false;
    if (currentItem.value.id !== undefined) {
      await axios.put(`http://localhost:8000/adminapi/competitions/${currentItem.value.id}`, updateForm);
      await getList();
    } else {
      console.error('Competition ID is undefined');
    }
  } catch (error) {
    console.error('Error updating competition:', error.response.data);
  }
};

const handleDelete = async (id) => {
  try {
    if (id !== undefined) {
      await axios.delete(`http://localhost:8000/adminapi/competitions/${id}`);
      await getList();
    } else {
      console.error('Competition ID is undefined');
    }
  } catch (error) {
    console.error('Error deleting competition:', error.response.data);
  }
};

const handleSearch = () => {
  getList(searchQuery.value);
}

const handleReset = () => {
  searchQuery.value = "";
  getList();
}
</script>
